/***************************************************************************
 *   Copyright (C) 2010 by programmeboy                                    *
 *   programmeboy@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
 ***************************************************************************/
 
#ifndef _COMMON__H__
#define _COMMON__H__

#include <ntifs.h>

#ifndef __cplusplus
#undef try
#undef except
#undef finally
#undef leave
#define try                         __try
#define except                      __except
#define finally                     __finally
#define leave                       __leave
#endif

#ifndef STATIC
#define STATIC static
#endif

#ifndef MAX_PATH
#define MAX_PATH 260
#endif

#define array_capacity(arr) (sizeof (arr) / sizeof ((arr)[0]))

//
//  defination for undocument function
//

typedef enum _SYSTEM_INFORMATION_CLASS
{
    SystemBasicInformation,                         // 0x00 SYSTEM_BASIC_INFORMATION
    SystemProcessorInformation,                     // 0x01 SYSTEM_PROCESSOR_INFORMATION
    SystemPerformanceInformation,                   // 0x02
    SystemTimeOfDayInformation,                     // 0x03
    SystemPathInformation,                          // 0x04
    SystemProcessInformation,                       // 0x05
    SystemCallCountInformation,                     // 0x06
    SystemDeviceInformation,                        // 0x07
    SystemProcessorPerformanceInformation,          // 0x08
    SystemFlagsInformation,                         // 0x09
    SystemCallTimeInformation,                      // 0x0A
    SystemModuleInformation,                        // 0x0B SYSTEM_MODULE_INFORMATION
    SystemLocksInformation,                         // 0x0C
    SystemStackTraceInformation,                    // 0x0D
    SystemPagedPoolInformation,                     // 0x0E
    SystemNonPagedPoolInformation,                  // 0x0F
    SystemHandleInformation,                        // 0x10
    SystemObjectInformation,                        // 0x11
    SystemPageFileInformation,                      // 0x12
    SystemVdmInstemulInformation,                   // 0x13
    SystemVdmBopInformation,                        // 0x14
    SystemFileCacheInformation,                     // 0x15
    SystemPoolTagInformation,                       // 0x16
    SystemInterruptInformation,                     // 0x17
    SystemDpcBehaviorInformation,                   // 0x18
    SystemFullMemoryInformation,                    // 0x19
    SystemLoadGdiDriverInformation,                 // 0x1A
    SystemUnloadGdiDriverInformation,               // 0x1B
    SystemTimeAdjustmentInformation,                // 0x1C
    SystemSummaryMemoryInformation,                 // 0x1D
    SystemMirrorMemoryInformation,                  // 0x1E
    SystemPerformanceTraceInformation,              // 0x1F
    SystemObsolete0,                                // 0x20
    SystemExceptionInformation,                     // 0x21
    SystemCrashDumpStateInformation,                // 0x22
    SystemKernelDebuggerInformation,                // 0x23
    SystemContextSwitchInformation,                 // 0x24
    SystemRegistryQuotaInformation,                 // 0x25
    SystemExtendServiceTableInformation,            // 0x26
    SystemPrioritySeperation,                       // 0x27
    SystemPlugPlayBusInformation,                   // 0x28
    SystemDockInformation,                          // 0x29
    SystemPowerInformationNative,                   // 0x2A
    SystemProcessorSpeedInformation,                // 0x2B
    SystemCurrentTimeZoneInformation,               // 0x2C
    SystemLookasideInformation,                     // 0x2D
    SystemTimeSlipNotification,                     // 0x2E
    SystemSessionCreate,                            // 0x2F
    SystemSessionDetach,                            // 0x30
    SystemSessionInformation,                       // 0x31
    SystemRangeStartInformation,                    // 0x32
    SystemVerifierInformation,                      // 0x33
    SystemAddVerifier,                              // 0x34
    SystemSessionProcessesInformation,              // 0x35
    SystemLoadGdiDriverInSystemSpaceInformation,    // 0x36
    SystemNumaProcessorMap,                         // 0x37
    SystemPrefetcherInformation,                    // 0x38
    SystemExtendedProcessInformation,               // 0x39
    SystemRecommendedSharedDataAlignment,           // 0x3A
    SystemComPlusPackage,                           // 0x3B
    SystemNumaAvailableMemory,                      // 0x3C
    SystemProcessorPowerInformation,                // 0x3D
    SystemEmulationBasicInformation,                // 0x3E
    SystemEmulationProcessorInformation,            // 0x3F
    SystemExtendedHanfleInformation,                // 0x40
    SystemLostDelayedWriteInformation,              // 0x41
    SystemBigPoolInformation,                       // 0x42
    SystemSessionPoolTagInformation,                // 0x43
    SystemSessionMappedViewInformation,             // 0x44
    SystemHotpatchInformation,                      // 0x45
    SystemObjectSecurityMode,                       // 0x46
    SystemWatchDogTimerHandler,                     // 0x47
    SystemWatchDogTimerInformation,                 // 0x48
    SystemLogicalProcessorInformation,              // 0x49
    SystemWo64SharedInformationObosolete,           // 0x4A
    SystemRegisterFirmwareTableInformationHandler,  // 0x4B
    SystemFirmwareTableInformation,                 // 0x4C
    SystemModuleInformationEx,                      // 0x4D
    SystemVerifierTriageInformation,                // 0x4E
    SystemSuperfetchInformation,                    // 0x4F
    SystemMemoryListInformation,                    // 0x50
    SystemFileCacheInformationEx,                   // 0x51
    SystemThreadPriorityClientIdInformation,        // 0x52
    SystemProcessorIdleCycleTimeInformation,        // 0x53
    SystemVerifierCancellationInformation,          // 0x54
    SystemProcessorPowerInformationEx,              // 0x55
    SystemRefTraceInformation,                      // 0x56
    SystemSpecialPoolInformation,                   // 0x57
    SystemProcessIdInformation,                     // 0x58
    SystemErrorPortInformation,                     // 0x59
    SystemBootEnvironmentInformation,               // 0x5A SYSTEM_BOOT_ENVIRONMENT_INFORMATION
    SystemHypervisorInformation,                    // 0x5B
    SystemVerifierInformationEx,                    // 0x5C
    SystemTimeZoneInformation,                      // 0x5D
    SystemImageFileExecutionOptionsInformation,     // 0x5E
    SystemCoverageInformation,                      // 0x5F
    SystemPrefetchPathInformation,                  // 0x60
    SystemVerifierFaultsInformation,                // 0x61
    MaxSystemInfoClass                              // 0x67

} SYSTEM_INFORMATION_CLASS, *PSYSTEM_INFORMATION_CLASS;

typedef struct _HANDLE_TABLE {

    ULONG_PTR TableCode;
    PEPROCESS QuotaProcess;
    HANDLE UniqueProcessId;

#define HANDLE_TABLE_LOCKS 4

    EX_PUSH_LOCK HandleTableLock[HANDLE_TABLE_LOCKS];

    LIST_ENTRY HandleTableList;

    EX_PUSH_LOCK HandleContentionEvent;
    PVOID DebugInfo;
    LONG ExtraInfoPages;
    ULONG FirstFree;
    ULONG LastFree;
    ULONG NextHandleNeedingPool;
    LONG HandleCount;

    union {
        ULONG Flags;
        BOOLEAN StrictFIFO : 1;
    };

} HANDLE_TABLE, *PHANDLE_TABLE;

typedef struct _HANDLE_TABLE_ENTRY_INFO {
    ACCESS_MASK AuditMask;
} HANDLE_TABLE_ENTRY_INFO, *PHANDLE_TABLE_ENTRY_INFO;

typedef struct _HANDLE_TABLE_ENTRY {

    union {
        PVOID Object;
        ULONG ObAttributes;
        PHANDLE_TABLE_ENTRY_INFO InfoTable;
        ULONG_PTR Value;
    };
    union {
        union {
            ACCESS_MASK GrantedAccess;
            struct {
                USHORT GrantedAccessIndex;
                USHORT CreatorBackTraceIndex;
            };
        };
        LONG NextFreeTableEntry;
    };
} HANDLE_TABLE_ENTRY, *PHANDLE_TABLE_ENTRY;

typedef struct _SYSTEM_PROCESS_INFORMATION {
    ULONG NextEntryOffset;
    ULONG NumberOfThreads;
    LARGE_INTEGER SpareLi1;
    LARGE_INTEGER SpareLi2;
    LARGE_INTEGER SpareLi3;
    LARGE_INTEGER CreateTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER KernelTime;
    UNICODE_STRING ImageName;
    KPRIORITY BasePriority;
    HANDLE UniqueProcessId;
    HANDLE InheritedFromUniqueProcessId;
    ULONG HandleCount;
    ULONG SessionId;
    ULONG_PTR PageDirectoryBase;
    SIZE_T PeakVirtualSize;
    SIZE_T VirtualSize;
    ULONG PageFaultCount;
    SIZE_T PeakWorkingSetSize;
    SIZE_T WorkingSetSize;
    SIZE_T QuotaPeakPagedPoolUsage;
    SIZE_T QuotaPagedPoolUsage;
    SIZE_T QuotaPeakNonPagedPoolUsage;
    SIZE_T QuotaNonPagedPoolUsage;
    SIZE_T PagefileUsage;
    SIZE_T PeakPagefileUsage;
    SIZE_T PrivatePageCount;
    LARGE_INTEGER ReadOperationCount;
    LARGE_INTEGER WriteOperationCount;
    LARGE_INTEGER OtherOperationCount;
    LARGE_INTEGER ReadTransferCount;
    LARGE_INTEGER WriteTransferCount;
    LARGE_INTEGER OtherTransferCount;
} SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;

NTSYSAPI
NTSTATUS
NTAPI
ZwQuerySystemInformation(
     IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
     OUT PVOID SystemInformation,
     IN ULONG SystemInformationLength,
     OUT PULONG ReturnLength
);

ObReferenceObjectByName(
    IN PUNICODE_STRING ObjectName,
    IN ULONG Attributes,
    IN PACCESS_STATE PassedAccessState OPTIONAL,
    IN ACCESS_MASK DesiredAccess OPTIONAL,
    IN POBJECT_TYPE ObjectType,
    IN KPROCESSOR_MODE AccessMode,
    IN OUT PVOID ParseContext OPTIONAL,
    OUT PVOID *Object
    );
extern POBJECT_TYPE IoDeviceObjectType;
extern POBJECT_TYPE IoDriverObjectType;

VOID 
WpOff();

VOID
WpOn();

extern PDRIVER_OBJECT gEEyesDriverObject;

#endif